package resilienceManager.qmul.gemom;
import inl.resilienceManager.qmul.gemom.TableSaver;

import java.io.File;
import java.io.FileInputStream;
import java.lang.*;
import java.util.Enumeration;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.helpers.DefaultHandler;

import cbr.resilienceManager.qmul.gemom.CbrData;

import com.sun.org.apache.xerces.internal.parsers.*; 



/**
 * A parser for the name space set
 * import the name space from the database
 * import the name space from the xml file 
 * 
 * COPYRIGHT DISCLAIMER
 * Synopsis: All the source codes presented in this document are intellectual
 * properties of Queen Mary, University of London.
 * 
 * @year 2010
 * @author Peng Jiang, John Bigham, Jiayi Wu Jinfu Wang------------QMUL
 * @version 1.0 
 */


public class nsParser {

	private SAXTreeBuilder saxTree = null;
	private static String file = "testTree.xml";
	DefaultMutableTreeNode rootNode = null;


	// the number of total namespace 
	int ns_number=0;


	// the case 
	CbrData cd;

	boolean saving=false;


	//private namespace[] namespaces;


	public nsParser(File xmlfile, CbrData cd){


		this.cd=cd;
		saving=true;


		DefaultMutableTreeNode top = new DefaultMutableTreeNode("root");
		saxTree = new SAXTreeBuilder(top);

		if(xmlfile.exists())
		{
			file=xmlfile.getAbsolutePath();
		}
		else{
			return; 
		}


		try{
			SAXParser saxParser = new SAXParser();
			saxParser.setContentHandler(saxTree);
			saxParser.parse(new InputSource(new FileInputStream(file)));
		}catch(Exception ex){
			ex.printStackTrace();
		}

	}

	public nsParser(File xmlfile){

		DefaultMutableTreeNode top = new DefaultMutableTreeNode("root");
		saxTree = new SAXTreeBuilder(top);

		if(xmlfile.exists())
		{
			file=xmlfile.getAbsolutePath();
		}
		else{
			return; 
		}


		try{
			SAXParser saxParser = new SAXParser();
			saxParser.setContentHandler(saxTree);
			saxParser.parse(new InputSource(new FileInputStream(file)));
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}

	public int getNS_number()
	{
		return this.ns_number;
	}

	public void buildNS(){
// 
//		XXX create namespace0 if not exists
		TableSaver.createNSTab(0);
		rootNode=saxTree.getTree();
		//DefaultMutableTreeNode node=(DefaultMutableTreeNode) rootNode.getFirstChild();
		ns_number=1;

		insertNS(ns_number,rootNode);

		visitAllNodes(rootNode);
	}


	public void insertNS(int id, DefaultMutableTreeNode n)
	{
		TreeNode[] s=(TreeNode[]) n.getPath();
		String scontent = null;
		for(int i=0; i<s.length; i++)
		{
			if(i==0)
			{
				scontent=s[i].toString();
				continue;
			}
			else
			{
				scontent=scontent+"/"+s[i].toString();
			}          		


		}
		if(saving){
			namespace nt=new namespace(ns_number,scontent);
			//save the namespace in the data based
//			if nt not in db
			if(Datasaver.findID(nt.getcontent(), cd.getTableID())<0)
			{
				Datasaver.insert(nt,cd.getTableID());
			}
		}
	}


	public void visitAllNodes(DefaultMutableTreeNode node) {

		if (node.getChildCount() >= 0) {
			for (Enumeration e=node.children(); e.hasMoreElements(); ) {
				DefaultMutableTreeNode n = (DefaultMutableTreeNode)e.nextElement();
//				if(n.toString().startsWith("meanRate")){
//					System.out.println(n);
//					node.remove(n);
////////			HashMap
//	
////					continue;
//				}
//				else{
//					ns_number++;
//
//					if(saving){
//						insertNS(ns_number,n);
//					}
//					visitAllNodes(n);
//				}
				
				ns_number++;

				if(saving){
				insertNS(ns_number,n);
				}
				visitAllNodes(n);
			}
		}

	}


	public DefaultMutableTreeNode getSAXTree()
	{

		//DefaultMutableTreeNode node=(DefaultMutableTreeNode) rootNode.getFirstChild();
		return rootNode;

	}


}
